
import pandas as pd
import time
import logging
import warnings
import os
import platform
import sys
from pathlib import Path
import numpy as np


def import_packages():
    import pandas as pd
    import numpy as np
    pd.set_option('display.width', 250)
    pd.set_option('display.expand_frame_repr', True)
    pd.set_option('display.max_rows', 10)
    pd.set_option('display.min_rows', 10)
    pd.set_option('display.max_columns', 500)
    pd.options.display.float_format = '{:.2f}'.format
    pd.options.mode.chained_assignment = None  # default='warn'
    import warnings
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    return pd, np


def set_log(namefile):
    paths = set_paths()
    format_ = '%(asctime)s (%(levelname)s) %(name)s: %(message)s'
    datetime_ = '%Y-%m-%d %H:%M:%S'
    logging.basicConfig(filename=paths.log / namefile, filemode='w', level=logging.DEBUG, format=format_, datefmt=datetime_)
    formatter = logging.Formatter(format_, datefmt=datetime_)
    streamer = logging.StreamHandler()
    streamer.setFormatter(formatter)
    streamer.setLevel(logging.INFO)
    logging.getLogger().addHandler(streamer)
    logger = logging.getLogger(__name__)
    logger.info('Python %s on %s' % (sys.version, sys.platform))
    logger.info('Platform node = %s' % (platform.node()))
    logger.info('{}'.format(namefile))
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    return logger


def set_paths():
    path_github = Path(*Path.cwd().parts[:Path.cwd().parts.index('ipn') + 1])
    path_dropbox, path_private_data, path_PATSTAT, path_factset, path_orbis = Path(''), Path(''), Path(''), Path(''), Path('')
    """
    TO EXECUTE ON YOUR LOCAL MACHINE, MODIFY AND UNCOMMENT LINES BELOW
    """
    if platform.node() == 'ADD_HERE':
        path_dropbox = Path('/ADD/PATH/TO/DATA_FILES/DIRECTORY/data_files')
        # NB: path_dropbox should be where the directory `data_files' is located. This is also where outputted data will be saved
        path_private_data = Path('/ADD/PATH/TO/PRIVATE_DATA/DIRECTORY/private_data')
        # NB: path_private_data should be where the directory `private_data' is located.
        # This is where the private data that could not be share should be stored
        path_PATSTAT = Path('/ADD/PATH/TO/PATSTAT/DATA')
        path_orbis = Path('/ADD/PATH/TO/ORBIS/DATA')
        path_factset = Path('/ADD/PATH/TO/FACTSET/DATA')
        # here we provide the option of specifying separately the paths to patstat, orbis and factset
        # i.e., the replicator does not necessarily need to place these datasets under the private_data directory
    paths = PathStorage(path_github, path_dropbox, path_private_data, path_PATSTAT, path_factset, path_orbis)
    return paths


class PathStorage:
    """Class for creating and storing paths to directories"""
    def __init__(self, path_github: Path, path_dropbox: Path, path_private_data: Path,
                 path_PATSTAT: Path, path_factset: Path, path_orbis: Path):
        self.PATSTAT = path_PATSTAT
        self.patstatglobal = self.PATSTAT / 'PATSTATGlobal2022'
        self.factset = path_factset
        self.orbis = path_orbis
        self.privatedata = path_private_data
        self.IEA = self.privatedata / 'IEA'
        self.manualorbis = self.privatedata / 'ManualExportFromOrbisPlatform'
        self.marklines = self.privatedata / 'Marklines'
        self.other = self.privatedata / 'Other'
        self.dropbox = path_dropbox
        self.rawdata = self.dropbox / 'raw_data'
        self.dataoutput = self.dropbox / 'Data_outputted'
        self.dataoutputtemp = self.dataoutput / 'temp'
        self.autoindustry = self.dataoutput / 'A_AutoIndustry'
        self.factsetvariables = self.dataoutput / 'B_FactsetVariables'
        self.patentvariables = self.dataoutput / 'C_PatentVariables'
        self.citations = self.patentvariables / 'citations'
        self.policy = self.dataoutput / 'D_Policy'
        self.analysis = self.dataoutput / 'E_Analysis'
        self.github = path_github
        self.output = self.github / 'output'
        self.log = self.github / 'output' / 'logs'
        self.tables = self.github / 'output' / 'tables'
        self.figures = self.github / 'output' / 'figures'
        self.figuressupplier_spillovers = self.figures / 'supplier_spillovers'
        self.figuresmainfigures = self.figures / 'mainfigures'
        self.figurestransport_nontransport = self.figures / 'transport_nontransport'
        self.figurescross_sectoral_spillovers = self.figures / 'cross_sectoral_spillovers'
        self.figuresoems = self.figures / 'oems'
        self.figuresfamily_counts_over_time = self.figures / 'family_counts_over_time'
        self.figurespolicies = self.figures / 'policies'
        self.figurescross_sectoral_spillovers = self.figures / 'cross_sectoral_spillovers'



def create_folderstructure(PATHS):
    list_dir = []
    list_dir.append(PATHS.output)
    list_dir.append(PATHS.log)
    list_dir.append(PATHS.figures)
    list_dir.append(PATHS.tables)
    list_dir.append(PATHS.dataoutput)
    list_dir.append(PATHS.dataoutputtemp)
    list_dir.append(PATHS.autoindustry)
    list_dir.append(PATHS.factsetvariables)
    list_dir.append(PATHS.patentvariables)
    list_dir.append(PATHS.policy)
    list_dir.append(PATHS.analysis)
    list_dir.append(PATHS.figuressupplier_spillovers)
    list_dir.append(PATHS.figuresmainfigures)
    list_dir.append(PATHS.figurestransport_nontransport)
    list_dir.append(PATHS.figurescross_sectoral_spillovers)
    list_dir.append(PATHS.figuresoems)
    list_dir.append(PATHS.figuresfamily_counts_over_time)
    list_dir.append(PATHS.figurespolicies)
    list_dir.append(PATHS.figurescross_sectoral_spillovers)
    for dir in list_dir:
        try:
            os.mkdir(dir)
        except:
            pass


# GLOBALS ------------------------
import_packages()
PATHS = set_paths()
create_folderstructure(PATHS)

